package chatBot.controller;

import chatBot.common.Result;

import chatBot.entity.Conversation;
import chatBot.entity.Message;
import chatBot.service.ConversationService;
import chatBot.utils.ContextHolderUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/gpt")
@Api(tags = "ChatGPT 会话管理")
public class ConversationController {

    @Resource
    private ConversationService conversationService;

    @ApiOperation("获取用户的会话列表")
    @GetMapping("/conversation")
    public Result<Object> getConversation() {
        return Result.success(conversationService.listConversations(ContextHolderUtil.getUserId())); // 返回结果
    }

    @ApiOperation("创建新会话")
    @PostMapping("/conversation")
    public Result<Object> createConversation(@RequestBody Conversation conversation) {
        return Result.success(conversationService.createConversation(conversation)); // 返回会话ID
    }

    @ApiOperation("删除会话及相关消息")
    @DeleteMapping("/conversation/{id}")
    public Result<Object> delete(@PathVariable("id") Long conversationId) {
        conversationService.removeConversation(conversationId); // 处理删除操作
        return Result.success();
    }

    @ApiOperation("获取会话的消息记录")
    @GetMapping("/message")
    public Result<Object> messagesOf(Long conversationId) {
        // 此处方法未实现，假设将来需要，可以添加合适的实现
              List<Message> messages = conversationService.messagesOfConversation(conversationId, null);
     return Result.success(messages);
    }

    @ApiOperation("重命名会话")
    @PutMapping("/conversation/rename")
    public Result<Object> renameConversation(@RequestBody Conversation conversation) {
        // 使用 conversationId 和 conversationName 来重命名会话
        Conversation updatedConversation = conversationService.renameConversation(
                conversation.getConversationId(), conversation.getConversationName());
        return Result.success(updatedConversation);
    }

}
